package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.PipelineStation;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.BPiplineDetail;
import cn.eonshine.cuba.lsms.modular.basic.service.IBPiplineDetailClientService;
import cn.eonshine.cuba.lsms.modular.basic.service.IBPiplineDetailService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
@Service
public class PiplineDetailClientService extends CommonService<PipelineStation> implements IBPiplineDetailClientService{
	
	@Autowired
	private IBPiplineDetailService iBPiplineDetailService;

	private final String listUrl = "/pipelineStation/list";
	private final String updateUrl = "/pipelineStation/update";
	
	@Override
	public void requestPiplineDetail() {
		requestData(listUrl, updateUrl);
	}
	
	@Override
	protected Object dataHandle(PipelineStation pipelineDetail) throws Exception {
		QueryWrapper<BPiplineDetail> entity = new QueryWrapper<BPiplineDetail>();
		entity.eq("ID", pipelineDetail.getStationCode());
		BPiplineDetail info = iBPiplineDetailService.getOne(entity);
		boolean isInsert = false;
		if(info == null) {
			info = new BPiplineDetail();
			isInsert = true;
		}
		
		info.setId(pipelineDetail.getStationCode());
//		BeanUtils.copyProperties(pipelineDetail, info);
		BeanUtil.copyProperties(pipelineDetail, info,true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
		info.setCreateBy(InfStatus.CREATE_BY);
		if(info.getCreateDate() == null) {
			info.setCreateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
		}
		info.setUpdateBy(InfStatus.UPDATE_BY);
		if(info.getUpdateDate() == null) {
			info.setUpdateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
		}
		
		if(isInsert) {
			iBPiplineDetailService.save(info);
		}else {
			iBPiplineDetailService.updateById(info);
		}
		
		return info;
	}
	@Override
	protected List<PipelineStation> feadBackDataHandle(Object obj, PipelineStation pipelineDetail, List<PipelineStation> feadBackDataLists) throws Exception {
		BPiplineDetail info = (BPiplineDetail) obj;
		PipelineStation tmpBack = new PipelineStation();
		tmpBack.setId(info.getId());
		tmpBack.setStationCode(pipelineDetail.getStationCode());
		tmpBack.setDataInputDatetime(pipelineDetail.getDataInputDatetime());
		tmpBack.setLsmsUpdate(BigDecimal.valueOf(InfStatus.LSMS_UPDATE_EXTRACT_YES));
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
}
